<?php

/* KLEODB MYSQL DRIVER */

	class kleodb {

		var $queries_count = 0;

		function connect($host, $user, $pass, $database, $port) {
			if (($link = mysql_connect($host, $user, $pass)) !== false) {
				$select = mysql_select_db($database);
				if($select){
					return $select;
				} else {
					die("oops -> KleoMysql Error - Your database name ({$database}) is wrong - Please, check your Mysql Connection");
				}
			} else { 
				die("oops -> KleoMysql Error - Please, Check Your Mysql Connection");
			}
		}

		function dbdie ($link) {
			if ($link)
				return mysql_close($link);
			else
				return false;
		}

		function query ($query, $optimus_mode=1) {
			$this->db_queries[] = $query;
			return mysql_query($query);
			if($optimus_mode == 1){
				return $this->db_free($query);
			}
			$this->queries_count++;
		}
	
		function escape_query_string($string) {
			return mysql_real_escape_string($string);
		}

		function drop($table, $if) {
			if($if == 0){
				return mysql_query("DROP TABLE IF EXISTS ".$table);
			} elseif($if == 1) {
				return mysql_query("DROP TABLE ".$table);
			}
		}

		function truncate($table) {
			return mysql_query("TRUNCATE TABLE ".$table);
		}

		function sql_error() {
			return mysql_error();
		}

		function db_free() {
			if ($result)
				return mysql_free_result($result);
		}

/*		//WE USE "query" FUNCTION FOR INSERT/UPDATE AND DELETE OPERATIONS
		function update($table, $op) {
			//NOTE: $op must be like this-> "SET item = 'item' WHERE itemid = 1 LIMIT 1
			return mysql_query('UPDATE '.$table.$op);
		}

		function insert($table, $op) {
			//NOTE: $op must be like this-> "VALUES aa, aa, aa 
			//  OR: ('aa','aaa') VALUES ('asd', 'asdf')
			return mysql_query('INSERT INTO '.$table.$op);
		}
*/

		function num_rows($query) {
			return mysql_num_rows($query);
		}

		function fetch_array($result) {
			return mysql_fetch_array($result);
		}

		function qresult($result){
			return mysql_result($result);
		}

		function pconnect($host, $user, $pass, $database, $port) {
			if (($link = mysql_pconnect($host, $user, $pass)) !== false) {
				return mysql_select_db($database);
			} else { 
				die("oops -> KleoMysql Error - Please Check Your Mysql Connection");
			}
		}

		function fetch_object($query){
			return mysql_fetch_object($result);
		}

		function show_databases(){
			return mysql_query("SHOW DATABASES");
		}

/*		//BETA 'activerecord-like' DB QUERIES
		function active_res($query, $array=null){
			
			if(!empty($array)){
				$array = exec($array);
				if(!is_array($array)){
					die("Active result error :|");
				}
			}
	
			$args = "";

			if(isset($array['where'])){
				$args .= "WHERE ".$array['where'];
			}

			if(isset($array['order'])){
				$args .= "ORDER BY ".$array['order'];
			}

			if(isset($array['by'])){
				$args .= $array['by'];
			}

			if(isset($array['limit'])){
				$args .= "LIMIT ".$array['limit'];
			}

			if(isset($array['order']) && !isset($array['by'])){
				die("ACTIVE RESULT ERROR");
			}

			if(!isset($array['order']) && isset($array['by'])){
				die("ACTIVE RESULT ERROR");
			}

			return mysql_query($query.$args);
		}
*/
	}



?>